﻿using System;
using System.Collections;
using System.Data;
using System.IO;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Reflection;
using AnyOffice.BIZ;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.Model;

namespace AnyOffice.general.notify.manage
{
    public partial class show_reader1 :  ModulePage
    {
        private string str;
        protected int readNum;
        protected StringBuilder strDept;
        protected string strTemp;
        protected int unreadNum;

        public show_reader1()
        {
            this.strDept = new StringBuilder();
            this.str = string.Empty;
            this.strTemp = string.Empty;
        }

        protected void btnClearReader_Click(object sender, EventArgs e)
        {
            new NotifyDao().ClearReaders(base.Conn, Convert.ToInt32(this.ViewState["NotifyId"].ToString()));
            base.CloseConn();
            Response.Redirect("show_reader.aspx?notify_id=" + this.ViewState["NotifyId"].ToString());
        }

        private void ShowReaderInfo()
        {
            string str = string.Empty;
            string str2 = string.Empty;
            DataTable table = new DepartmentDao().SelectParentAll(base.Conn);
            if (table.Rows.Count > 0)
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    this.strDept.Append("<tr class=\"TableData\">\n");
                    this.strDept.Append("<td>\n");
                    this.strDept.Append("|─" + table.Rows[i]["DeptName"].ToString());
                    this.strDept.Append("</td>\n");
                    str = string.Empty;
                    str2 = string.Empty;
                    DataTable table2 = new UsersDao().SelectBaseByDeptId(base.Conn, Convert.ToInt32(table.Rows[i]["DeptId"].ToString()));
                    if (table2.Rows.Count > 0)
                    {
                        for (int j = 0; j < table2.Rows.Count; j++)
                        {
                            if (this.str.IndexOf("," + table2.Rows[j]["UserId"].ToString() + ",") >= 0)
                            {
                                this.readNum++;
                                str = str + table2.Rows[j]["UserName"].ToString() + ",";
                            }
                            else
                            {
                                this.unreadNum++;
                                str2 = str2 + table2.Rows[j]["UserName"].ToString() + ",";
                            }
                        }
                    }
                    if (str.EndsWith(","))
                    {
                        str = str.Substring(0, str.Length - 1);
                    }
                    if (str2.EndsWith(","))
                    {
                        str2 = str2.Substring(0, str2.Length - 1);
                    }
                    this.strDept.Append("<td>\n");
                    this.strDept.Append(str);
                    this.strDept.Append("</td>\n");
                    this.strDept.Append("<td>\n");
                    this.strDept.Append(str2);
                    this.strDept.Append("</td>");
                    this.strDept.Append("</tr>\n");
                    this.strTemp = "";
                    this.BindDeptTree(int.Parse(table.Rows[i]["DeptId"].ToString()), this.strTemp);
                }
            }
        }
        private void BindDeptTree(int deptId, string strTemp)
        {
            string str = string.Empty;
            string str2 = string.Empty;
            DataTable table = new DepartmentDao().SelectChildAll(base.Conn, deptId);
            if (table.Rows.Count > 0)
            {
                strTemp = strTemp + "|　";
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    this.strDept.Append("<tr class=\"TableData\">\n");
                    this.strDept.Append("<td>\n");
                    this.strDept.Append(strTemp + "|─" + table.Rows[i]["DeptName"].ToString());
                    this.strDept.Append("</td>\n");
                    str = string.Empty;
                    str2 = string.Empty;
                    DataTable table2 = new UsersDao().SelectBaseByDeptId(base.Conn, Convert.ToInt32(table.Rows[i]["DeptId"].ToString()));
                    if (table2.Rows.Count > 0)
                    {
                        for (int j = 0; j < table2.Rows.Count; j++)
                        {
                            if (this.str.IndexOf("," + table2.Rows[j]["UserId"].ToString() + ",") >= 0)
                            {
                                this.readNum++;
                                str = str + table2.Rows[j]["UserName"].ToString() + ",";
                            }
                            else
                            {
                                this.unreadNum++;
                                str2 = str2 + table2.Rows[j]["UserName"].ToString() + ",";
                            }
                        }
                    }
                    if (str.EndsWith(","))
                    {
                        str = str.Substring(0, str.Length - 1);
                    }
                    if (str2.EndsWith(","))
                    {
                        str2 = str2.Substring(0, str2.Length - 1);
                    }
                    this.strDept.Append("<td>\n");
                    this.strDept.Append(str);
                    this.strDept.Append("</td>\n");
                    this.strDept.Append("<td>\n");
                    this.strDept.Append(str2);
                    this.strDept.Append("</td>");
                    this.strDept.Append("</tr>\n");
                    this.BindDeptTree(int.Parse(table.Rows[i]["DeptId"].ToString()), strTemp);
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                int notifyId = (Request.QueryString["notify_id"] == null) ? 0 : int.Parse(Request.QueryString["notify_id"].ToString());
                this.ViewState["NotifyId"] = notifyId;
                DataTable table = new NotifyDao().SelectByNotifyId(base.Conn, notifyId, base.User.Identity.Name);
                if (table.Rows.Count > 0)
                {
                    this.litSubject.Text = table.Rows[0]["Subject"].ToString();
                    this.litTypeName.Text = table.Rows[0]["TypeName"].ToString();
                    this.litUserName.Text = "<u title=\"部门：" + table.Rows[0]["DeptName"].ToString() + "\" style=\"CURSOR:hand\">" + table.Rows[0]["UserName"].ToString() + "</u>";
                    this.litSendTime.Text = (table.Rows[0]["SendTime"] != DBNull.Value) ? table.Rows[0]["SendTime"].ToString() : "";
                    if (table.Rows[0]["Readers"] != DBNull.Value)
                    {
                        this.str = "," + table.Rows[0]["Readers"].ToString() + ",";
                    }
                }
                this.btnClearReader.Attributes.Add("onclick", "delete_reader();");
                this.ShowReaderInfo();
            }
        }

 
    }
}
